import _ from '../../utils/under-dash';
import { xmlDecode, isDateFmt, excelToDate } from '../../utils/utils';
import colCache from '../../utils/col-cache';
import Dimensions from '../../doc/range';

import Row from '../../doc/row';
import Column from '../../doc/column';

class WorksheetReader {
    constructor({ workbook, id, iterator, options }) {

        this.workbook = workbook;
        this.id = id;
        this.iterator = iterator;
        this.options = options || {};

        // and a name
        this.name = `Sheet${this.id}`;

        // column definitions
        this._columns = null;
        this._keys = {};

        // keep a record of dimensions
        this._dimensions = new Dimensions();
    }

    // destroy - not a valid operation for a streaming writer
    // even though some streamers might be able to, it's a bad idea.
    destroy() {
        throw new Error('Invalid Operation: destroy');
    }

    // return the current dimensions of the writer
    get dimensions() {
        return this._dimensions;
    }

    // Columns

    // get the current columns array.
    get columns() {
        return this._columns;
    }

    // get a single column by col number. If it doesn't exist, it and any gaps before it
    // are created.
    getColumn(c) {
        if (typeof c === 'string') {
            // if it matches a key'd column, return that
            const col = this._keys[c];
            if (col) {
                return col;
            }

            // otherise, assume letter
            c = colCache.l2n(c);
        }
        if (!this._columns) {
            this._columns = [];
        }
        if (c > this._columns.length) {
            let n = this._columns.length + 1;
            while (n <= c) {
                this._columns.push(new Column(this, n++));
            }
        }
        return this._columns[c - 1];
    }

    getColumnKey(key) {
        return this._keys[key];
    }

    setColumnKey(key, value) {
        this._keys[key] = value;
    }

    deleteColumnKey(key) {
        delete this._keys[key];
    }

    eachColumnKey(f) {
        _.each(this._keys, f);
    }

    async read() {

    }
}

export default WorksheetReader;
